package blockchain.crypto;

import com.xiaoleilu.hutool.crypto.digest.DigestUtil;
import org.springframework.security.crypto.codec.Hex;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SHAUtil {
    /**
     * 利用Apache commons-codec 的工具类实现SHA-256加密
     *
     * @param originalStr 加密前的报文
     * @return String 加密后的报文
     */
    public static String getSHA256BasedMD(String originalStr) {
        MessageDigest messageDigest;
        String encdeStr = "";
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
            byte[] hash = messageDigest.digest(originalStr.getBytes("UTF-8"));
            encdeStr = new String(Hex.encode(hash));
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return encdeStr;
    }

    /**
     * 利用Hutool工具类实现SHA-256加密
     *
     * @param originalStr 加密前的报文
     * @return 加密后的报文
     */
    public static String sha256BasedHutool(String originalStr) {
        return DigestUtil.sha256Hex(originalStr);
    }
}
